<?php
class Registration extends Admin_Controller
{
	public $col_not_price = array(12,14,15,17,19,20,32,34,35,37,39,40,47,49,50);
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('voucher/fund_model', 'fund');
		$this->load->model('register_model','register');
		$this->load->model('register_detail_model','register_detail');
		$this->load->model('register_title_model','register_title');
		$this->load->model('register_column_model','register_column');
	}
	
	public function index($fund_id, $year, $month, $agency_id = NULL)
	{
		$agency_id = (empty($agency_id)) ? login_data('agency_id') : $agency_id;
		// ค้นหา
		if($_GET)
		{
			$agency_id = (empty($_GET['agency_id'])) ? NULL : '/'.$_GET['agency_id'];
			redirect('registration/index/'.$fund_id.'/'.$_GET['year'].'/'.$_GET['month'].$agency_id);
			exit;
		}
		$data['fund_id'] = $fund_id;
		$data['agency_id'] = $agency_id;
		$data['year'] = $year;
		$data['month'] = $month;
		$year = Fund::convert_to_real_year(($year - 543), $month);
		$data['fund'] = $this->fund->get_row($fund_id);
		$data['result'] = $this->register->get_register($fund_id, $agency_id, $year, $month);
		$data['cols'] = $this->register_column->select('ID')->where('TITLE_ID IS NOT NULL AND PARENT_ID <> 0')->get(FALSE, FALSE);
		$data['sum_month'] = $this->register->get_sum($fund_id, $agency_id, $year, $month); // ผลรวมรายเดือน
		$data['sum_year'] = $this->register->get_sum($fund_id, $agency_id, $year, $month, TRUE); // ผลรวมตั้งแต่ต้นปี
		
		$this->template->append_metadata(js_print());
		$this->template->build('index',$data);
	}
	
	public function form($fund_id, $year, $month, $agency_id, $id = NULL)
	{
		$this->check_fisrt_start();
		$data['fund_id'] = $fund_id;
		$data['agency_id'] = $agency_id;
		$data['year'] = $year;
		$data['month'] = $month;
		$data['rs'] = $this->register->get_row($id);	
		$data['price'] = $id ? $this->get_register_column($fund_id, $data['rs']['title_id'], $data['rs']) : NULL;
		$this->template->append_metadata(js_datepicker());
		$this->template->build('form',$data);
	}
	
	public function save()
	{
		if($_POST)
		{	//$this->db->debug = true;
			/*
			 * บันทึกลงทะเบียนคุมเงิน
			 */
			$data = array(
			'id' => $_POST['id'],
			'fund_id' => $_POST['fund_id'],
			'agency_id' => login_data('agency_id'),
			'user_id' => login_data('id'),
			'date_record' => date_to_mysql($_POST['date_record'], TRUE),
			'number' => $_POST['number'],
			'title_id' => $_POST['title_id'],
			'side' => $_POST['side'],
			'sub' => $_POST['sub'],				
			'ps' => $_POST['ps']
			);
			$regiser_id = $this->register->save($data);
			/*
			 * บันทึกรายละเอียดทะเบียนคุมเงิน 
			 */
			//$this->register_detail->delete('register_id', $regiser_id);
			$this->db->execute('DELETE FROM REGISTER_DETAILS WHERE REGISTER_id = '.$regiser_id);
			foreach($_POST['price'] as $col => $item)
			{
				$data_detail = array(
				'register_id' => $regiser_id,
				'col' => $col,
				'price' => $_POST['price'][$col],
				'is_price' => $_POST['is_price'][$col]
				);
				$this->register_detail->save($data_detail);
			}
			
			if(empty($_POST['id']))
			{
				add_log('เพิ่มทะเบียนคุมเงิน กองทุน  "'.$this->fund->get_one('name',$_POST['fund_id']).'" วันเดือนปีที่ลงบัญชี '.$_POST['date_record']);
			}
			else
			{
				add_log('แก้ไขทะเบียนคุมเงิน กองทุน  "'.$this->fund->get_one('name',$_POST['fund_id']).'" วันเดือนปีที่ลงบัญชี '.$_POST['date_record']);
			}
			
		}
		list($d, $m, $y) = explode('/', $_POST['date_record']);
		redirect('registration/index/'.$_POST['fund_id'].'/'.Fund::convert_to_year_budget($y, $m).'/'.$m);
	}
	
	public function delete($fund_id, $year, $month, $id)
	{
		if($id)
		{
			add_log('ลบทะเบียนคุมเงิน กองทุน  "'.$this->fund->get_one('name',$_POST['fund_id']));
			$this->register->delete($id);
			$this->register_detail->delete('register_id', $id);
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('registration/index/'.$fund_id.'/'.$year.'/'.$month);
	}
	
	public function check_new_month()
	{
		$date = date_to_mysql($_POST['date_record'], TRUE);
		$result = $this->db->getone("SELECT REGISTERS.ID FROM REGISTERS WHERE YEAR(REGISTERS.DATE_RECORD) = YEAR('$date') AND MONTH(REGISTERS.DATE_RECORD) = MONTH('$date')");
		echo  isset($result) ? 'true' : 'false';
	}
		
	public function get_register_column($fund_id, $title_id, $rs = NULL)
	{
		$agency_id = login_data('agency_id');
			$side = array('1' => 'รับ', '2' => 'จ่าย');
			$sub = array('1' => 'รับโอนจากส่วนกลาง', '2' => 'รายได้', '3' => 'รับคืน', '4' => 'โอนกลับส่วนกลาง', '5' => 'ค่าใช้จ่าย');
						
			$cols = $this->register_column
			->select('register_columns.*, register_titles.title')
			->join('join register_titles on register_titles.id = register_columns.title_id')
			->where('title_id = '.$title_id)->get();
			
			$result = '<table class="form-inner">';
			// parent
			$result .= '<tr class="sub"><td colspan="2">';
			$result .= $side[$cols[0]['side']].' &raquo; '.$sub[$cols[0]['sub']].' &raquo; '.$cols[0]['title'];
			$result .= form_hidden('title_id',$cols[0]['title_id']);
		    $result .= form_hidden('side',$cols[0]['side']);
			$result .= form_hidden('sub',$cols[0]['sub']);
			$result .= '</td></tr>';
			
			// child
			$col_datas = $this->db->getassoc('SELECT COL, PRICE FROM REGISTER_DETAILS WHERE REGISTER_ID = ?', array($rs['id']));
			foreach($cols as $item) 
			{
				$value = (!empty($col_datas[$item['col_number']])) ? 'value="'.number_format($col_datas[$item['col_number']], 2).'"' : ''; 
				$is_price = (in_array($item['col_number'], $this->col_not_price) ) ? form_hidden('is_price['.$item['col_number'].']', 0) : form_hidden('is_price['.$item['col_number'].']', 1);
				$result .= '<tr><th>'.$item['col'].'</th><td><input type="text" name="price['.$item['col_number'].']" class="number" '.$value.' />'.$is_price.'</td></tr>';
			}
			
			
			$result .= '</table>';
			
			if($rs) return $result; 
			else echo $result; 
		
	}

	public function delete_all($fund_id, $agency_id)
	{
		Fund::delete_all($fund_id, $agency_id);
	}
	
}
?>